<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>用户管理页面</title>
    <script src="/jquery-3.6.0.js"></script>
    <script src="/layui/layui.js"></script>
    <link href="/layui/css/layui.css" rel="stylesheet"/>
</head>
<body>
<!--修改表单-->
<form class="layui-form" action="" style="display: none"  id="editTester" lay-filter="editTesterFormFilter">
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">id</label>
        <div class="layui-input-inline">
            <input type="text" name="id" required lay-verify="required"  autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="name" required lay-verify="required" placeholder="请输入姓名" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-inline">
            <input type="text" name="phone" required lay-verify="required phone" placeholder="请输入手机号" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">测试时间</label>
        <div class="layui-input-inline">
            <input type="text" name="testTime" id="testTime" required lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit lay-filter="editTesterFilter">立即提交</button>
        </div>
    </div>
</form>
<!--查询-->
<form class="layui-form" action="" id="search" lay-filter="searchTesterFormFilter">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="name"  placeholder="请输入姓名" autocomplete="off"
                   class="layui-input" >
        </div>
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-inline">
            <input type="text" name="phone"   placeholder="请输入手机号" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" lay-submit lay-filter="search">查询</button>
            <button type="reset" id="searchReset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<table id="tester" lay-filter="testerFilter"></table>
</body>
<!--行内按钮-->
<script type="text/html" id="operation">
    <a shiro:hasPermission="character:tester:del" class="layui-btn layui-btn-dange layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
    <a shiro:hasPermission="character:tester:edit" class="layui-btn layui-btn-xs " lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
</script>
<!--工具栏按钮-->
<script type="text/html" id="toolbar">
    <a shiro:hasPermission="character:tester:del" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delBatch"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script>
    layui.use(['table','form','layer','laydate'], function() {
        let table = layui.table;
        let form = layui.form;
        let layer = layui.layer;
        let date = layui.laydate;
        //第一个实例
        table.render({
            elem: '#tester'
            , height: 400
            , url: '/tester' //数据接口
            , page: true//开启分页
            , limit: 5
            , limits: [5, 10, 15, 20]
            , toolbar: '#toolbar'
            , cols: [
                [ //表头
                    {field: 'select', width: 50, type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', width: 80, sort: true}
                    , {field: 'name', title: '姓名', width: 120}
                    , {field: 'phone', title: '手机号', width: 120}
                    , {field: 'testTime', title: '测试时间', width: 240}
                    , {field: 'operation', title: '操作', toolbar: '#operation'}
                ]
            ]
        });
        date.render({
            elem: '#testTime',
            type: 'datetime'
        });
        //工具栏按钮添加和批量删除
        table.on('toolbar(testerFilter)', function (obj) {
            let check = table.checkStatus(obj.config.id);
            let data = check.data;
            let layEvent = obj.event;
            if (layEvent === 'delBatch'){
                if(data.length === 0){
                    layer.msg("请选择一行数据");
                }else {
                    let idList = [];
                    for(let i=0;i<data.length;i++){
                        idList[i] = data[i].id;
                    }
                    layer.confirm('真的删除吗',function (index){
                        $.ajax({
                            url:"/tester/delete",
                            type:"delete",
                            data:{idList:idList},
                            dataType:"json",
                            success:function (result){
                                if(result.code == '0'){
                                    layer.closeAll();
                                    table.reload('tester',{
                                        page:{
                                            curr:1
                                        }
                                    });
                                    layer.msg(result.msg,{icon:6});
                                }else {
                                    layer.msg(result.msg,{icon:5});
                                }
                            }
                        })
                    })
                }
            }
        })
        //行内按钮修改删除
        table.on('tool(testerFilter)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            if (layEvent === 'edit') {
                form.val('editTesterFormFilter',{
                    id:data.id,
                    name:data.name,
                    phone:data.phone,
                    testTime:data.testTime
                })
                layer.open({
                    title: '修改用户',
                    type: 1,
                    area: ['550px', '400px'],
                    offset: '200px',
                    content: $("#editTester")
                });
                // 栏添加按钮弹出表单提交按钮触发事件
                form.on('submit(editTesterFilter)', function (data) {
                    $.ajax({
                        url: "/tester/update",
                        contentType:'application/json;charset=UTF-8',
                        data: JSON.stringify(data.field),
                        dataType: "json",
                        type: "put",
                        success: function (result) {
                            if (result.code == '0') {
                                layer.closeAll();
                                $("#editTester")[0].reset();
                                form.render();
                                table.reload('tester');
                                layer.msg(result.msg, {icon: 6});
                            } else {
                                layer.msg(result.msg, {icon: 5});
                            }
                        }
                    })
                })
            }else if (layEvent === 'del'){
                if(data.length === 0){
                    layer.msg("请选择一行数据");
                }else {
                    layer.confirm('真的删除吗',function (index){
                        let idList = [];
                        idList[0] = data.id;
                        $.ajax({
                            url:"/tester/delete",
                            type:"delete",
                            data:{idList:idList},
                            dataType:"json",
                            success:function (result){
                                if(result.code == '0'){
                                    layer.closeAll();
                                    table.reload('tester',{
                                        page:{
                                            curr:1
                                        }
                                    });
                                    layer.msg(result.msg,{icon:6});
                                }else {
                                    layer.msg(result.msg,{icon:5});
                                }
                            }
                        })
                    })
                }
            }
        })
        //查询
        form.on('submit(search)', function (data) {
            table.reload('tester', {
                where: {
                    name: data.field.name,
                    phone: data.field.phone,
                    page: 1,
                    limit: 5
                }
            });
            return false;
        });
        //重置按钮
        $(document).on('click', '#searchReset', function () {
            table.reload('tester', {
                where: {
                    name: null,
                    phone: null
                }
            })
        });
    });
</script>
</html>